﻿Imports System.IO


Public Class Frm_Papier
    Public avo As Pappier
    Public dv As Dossier
    Private Sub Btn_parc_marji3ona_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Me.Enabled = False
        Frm_Rech_Dossier.Show()
        Partager.type = "Frm_Papier"
    End Sub

    Private Sub btn_ajout_morfa9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_ajout_morfa9.Click
        If Comb_marji3ona.SelectedIndex < 0 Then
            MsgBox("اختر الملف أولا", MsgBoxStyle.Information, "خطأ")
        Else
            'Partager.hadf = "ajo"
            Partager.isTelClicked = True
            Me.Enabled = False
            '
            Frm_Ajout_Morfa9.List_morfa9.Items.Clear()
            Partager.list_piece.Clear()
            'If List_mostanad.SelectedIndices.Count > 0 Then
            '    For Each pj As Jouinture In avo.List_jouiture
            '        Partager.list_piece.Add(pj)
            '    Next
            'End If
            Frm_Ajout_Morfa9.Show()
        End If

    End Sub

    Private Sub Btn_rech_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_rech.Click
        If Comb_marji3ona.SelectedIndex >= 0 Then
            Me.Enabled = False
            Frm_Rech_Papier.Show()
        End If
    End Sub

    Private Sub Frm_Papier_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Partager.close()
        Try
            Partager.nonInitial = New ArrayList
            Partager.nonInitial.Add("Comb_marji3ona")
            Partager.remplirDossier(Comb_marji3ona)
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub



    Private Sub Btn_Modif_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_Modif.Click
        If List_mostanad.SelectedIndices.Count > 0 Then

            If (txt_mostanad.Text <> "" And txt_nbr.Text <> "") Then
                If MsgBox("هل تريد فعلا تعديل معلومات هذا المستند ؟", MsgBoxStyle.YesNo Or MsgBoxStyle.Information, "تعديل المستندات") = MsgBoxResult.Yes Then

                    Try
                        Partager.close()
                        Dim cli As New Pappier(1, txt_mostanad.Text, txt_nbr.Text, txt_molahadat.Text)
                        dv.modifier_pappier(cli, avo.id)
                        cli.id = avo.id

                        If Partager.list_piece.Count > 0 Or Partager.isTelClicked Then
                            'cli.suprimmer_jouiture(avo.id)
                            Dim i As Integer = 0
                            For Each Piece As Jouinture In Partager.list_piece
                                cli.Ajouter_jouiture(Piece)
                                Piece = cli.List_jouiture(cli.List_jouiture.Count - 1)

                                My.Computer.FileSystem.CopyFile( _
                                Partager.lstChemin(i), _
                                Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) & "PieceJointe\" & Piece.Nom & Piece.Id & "." & Piece.ex, _
                                FileIO.UIOption.OnlyErrorDialogs, _
                                FileIO.UICancelOption.DoNothing)
                                i += 1
                            Next
                            Partager.lstChemin = Nothing
                        End If

                        Partager.list_piece.Clear()
                        List_morfa9.Items.Clear()
                        List_mostanad.Items.Clear()
                        For Each khib As Pappier In dv.List_pappier
                            List_mostanad.Items.Add(New ListViewItem(New String() {khib.Nom, khib.Nombre}))
                        Next
                        MsgBox("عملية التعديل تمت بنجاح", MsgBoxStyle.Information, "نجاح")
                        Partager.vider(Me, partager.noninitial)
                        List_morfa9.Items.Clear()
                    Catch ex As Exception

                        MsgBox(ex.Message)

                    End Try
                End If

            Else
                MsgBox("أدخل البيانات من فضلك", MsgBoxStyle.Critical, "! خـطأ")
            End If
        Else
            MsgBox("اختر مستند من القائمة", MsgBoxStyle.Information, "خطأ في التعديل")
        End If
    End Sub

    Private Sub Btn_supp_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_supp.Click
        Partager.close()
        If List_morfa9.SelectedIndices.Count <= 0 And List_mostanad.SelectedIndices.Count > 0 Then
            Try
                Dim clt As Pappier = dv.List_pappier(List_mostanad.SelectedIndices.Item(0))
                dv.suprimmer_pappier(avo)
                List_mostanad.Items.Clear()
                For Each khib As Pappier In dv.List_pappier
                    List_mostanad.Items.Add(New ListViewItem(New String() {khib.Nom, khib.Nombre}))
                Next
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
            Partager.vider(Me, partager.noninitial)
            List_morfa9.Items.Clear()
        ElseIf List_morfa9.SelectedIndices.Count > 0 Then
            Try
                Dim clt As Jouinture = avo.List_jouiture(List_morfa9.SelectedIndices.Item(0))
                avo.suprimmer_jouitureDirect(clt)

                List_morfa9.Items.Clear()
                For Each khib As Jouinture In avo.List_jouiture
                    List_morfa9.Items.Add(New ListViewItem(New String() {khib.Nom}))
                Next
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
            Dim selectedIndice As Integer = List_mostanad.SelectedIndices.Item(0)
            Partager.vider(Me, partager.noninitial)
            List_mostanad.Items(selectedIndice).Selected = True
        End If

    End Sub


    Private Sub btn_Ajout_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Ajout.Click
        Try
            If Comb_marji3ona.SelectedIndex < 0 Then
                MsgBox("اختر الملف أولا", MsgBoxStyle.Information, "خطأ")
            Else
                Partager.close()
                If (List_mostanad.SelectedIndices.Count > 0) Then
                    MsgBox("عفوا لا يمكنك الإضافة، لإضافة مستند جديد عليك إزالة التحديد عن المستند", MsgBoxStyle.Critical, "خطأ في الإضافة")
                Else

                    If (txt_mostanad.Text <> "" And txt_nbr.Text <> "") Then

                        If MsgBox("هل تريد فعلا إضافة هذا المستند ؟", MsgBoxStyle.YesNo Or MsgBoxStyle.Information, "إضافة المستندات") = MsgBoxResult.Yes Then
                            Try
                                Partager.close()
                                Dim cli As New Pappier(1, txt_mostanad.Text, txt_nbr.Text, txt_molahadat.Text)
                                dv.Ajouter_pappier(cli)

                                cli = CType(dv.List_pappier.Item(dv.List_pappier.Count - 1), Pappier)
                                Dim i As Integer = 0
                                For Each Piece As Jouinture In Partager.list_piece
                                    cli.Ajouter_jouiture(Piece)
                                    Piece = cli.List_jouiture(cli.List_jouiture.Count - 1)

                                    My.Computer.FileSystem.CopyFile( _
                                    Partager.lstChemin(i), _
                                    Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) & "PieceJointe\" & Piece.Nom & Piece.Id & "." & Piece.ex, _
                                    FileIO.UIOption.OnlyErrorDialogs, _
                                    FileIO.UICancelOption.DoNothing)
                                    i += 1
                                Next
                                Partager.lstChemin = Nothing

                                List_mostanad.Items.Clear()
                                For Each khib As Pappier In dv.List_pappier
                                    List_mostanad.Items.Add(New ListViewItem(New String() {khib.Nom, khib.Nombre}))
                                Next
                                Partager.list_piece.Clear()
                                MsgBox("تمت إضافة المستند بنجاح", MsgBoxStyle.Information, "نجاح")
                                Partager.vider(Me, Partager.nonInitial)
                            Catch ex As Exception
                                MsgBox(ex.Message)
                            End Try
                        End If
                    Else
                        MsgBox("أدخل البيانات من فضلك", MsgBoxStyle.Critical, "! خـطأ")
                    End If
                End If
            End If
        Catch ex As Exception

            MsgBox(ex.Message)

        End Try
    End Sub

    Private Sub List_mostanad_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles List_mostanad.SelectedIndexChanged
        Try
            Partager.close()
            For Each i As Integer In List_mostanad.SelectedIndices
                avo = dv.List_pappier(i)

                txt_mostanad.Text = avo.Nom
                txt_nbr.Text = avo.Nombre
                txt_molahadat.Text = avo.Remarque

                Try
                    avo.remplir_List_jouiture()
                    List_morfa9.Items.Clear()
                    For Each khib As Jouinture In avo.List_jouiture
                        List_morfa9.Items.Add(New ListViewItem(New String() {khib.Nom}))
                    Next
                Catch ex As Exception
                    MsgBox(ex.Message)
                End Try
            Next
            If List_mostanad.SelectedIndices.Count <= 0 Then
                Partager.vider(Me, partager.noninitial)
                List_morfa9.Items.Clear()
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub btn_eporter_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_eporter.Click
        If List_morfa9.SelectedItems.Count > 0 Then
            Dim khib As Jouinture = avo.List_jouiture(List_morfa9.SelectedIndices.Item(0))
            SaveFileDialog1.FileName = khib.Nom
            SaveFileDialog1.ShowDialog()
        End If
    End Sub

    Private Sub SaveFileDialog1_FileOk(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles SaveFileDialog1.FileOk
        Try
            Dim Piece As Jouinture = avo.List_jouiture(List_morfa9.SelectedIndices.Item(0))
            Dim img As Byte() = Partager.GetPhoto(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) & "PieceJointe\" & Piece.Nom & Piece.Id & "." & Piece.ex)
            File.WriteAllBytes(SaveFileDialog1.FileName & "." & Piece.ex, img)

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub txt_nbr_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txt_nbr.KeyPress
        Dim allowedChars As String = "1234567890"
        If e.KeyChar = ChrW(8) Then
            e.Handled = False
        ElseIf allowedChars.IndexOf(e.KeyChar) = -1 Then
            ' Invalid Character
            e.Handled = True
        End If
    End Sub

    Private Sub btn_Ouvrir_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOuvrir.Click
        If List_morfa9.SelectedItems.Count > 0 Then

            Try
                Dim Piece As Jouinture = avo.List_jouiture(List_morfa9.SelectedIndices.Item(0))
                Process.Start(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) & "PieceJointe\" & Piece.Nom & Piece.Id & "." & Piece.ex)
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try

        End If
    End Sub

    Private Sub List_morfa9_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles List_morfa9.DoubleClick
        Try
            Dim Piece As Jouinture = avo.List_jouiture(List_morfa9.SelectedIndices.Item(0))

            Process.Start(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) & "PieceJointe\" & Piece.Nom & Piece.Id & "." & Piece.ex)
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

    End Sub


    Private Sub Comb_marji3ona_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Comb_marji3ona.SelectedIndexChanged
        Try
            Partager.vider(Me, Partager.nonInitial)
            Partager.deMarji = DirectCast(Comb_marji3ona.SelectedItem, DictionaryEntry)
            For Each d As Dossier In Partager.moi.List_info_fichier
                If d.id = CInt(Partager.deMarji.Key) Then
                    dv = d
                    dv.remplir_List_pappier()
                End If
            Next
            List_mostanad.Items.Clear()
            For Each khib As Pappier In dv.List_pappier
                List_mostanad.Items.Add(New ListViewItem(New String() {khib.Nom, khib.Nombre}))
            Next
            Partager.vider(Me, partager.noninitial)
            List_morfa9.Items.Clear()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub Btn_Rech_Doss_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_Rech_Doss.Click
        Me.Enabled = False
        Partager.frmafect = Me
        Frm_Recherche.Show()
        Partager.type = Me.Name
    End Sub
  
End Class